package com.code.generator.cn.util.base;




import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.util.PathUtil;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtil {

    public static Map<String, Object> createFieldMap(CoderField coderField, Map<String,Object> createMap) {
        Map<String,Object> fieldMap=new HashMap<>();
        //设置字段名称
        fieldMap.put("name",coderField.getClassFieldName());
        //设置字段的备注
        fieldMap.put("remark",coderField.getFieldRemark());
        //设置字段的类型
        fieldMap.put("type",MyStringChangeUtil.changeFileld(coderField.getFieldType()));
        //upName 设置首字母大写
        fieldMap.put("upName",MyStringChangeUtil.changeField(coderField.getFieldName(),3));

        fieldMap.put("isPrimary",coderField.getIsPrimary());
        fieldMap.put("fieldName",coderField.getFieldName());
        fieldMap.put("isNull",coderField.getIsNull());
        fieldMap.put("isSearch",coderField.getIsSearch());
        fieldMap.put("isTable",coderField.getIsTable());
        fieldMap.put("isView",coderField.getIsView());
        fieldMap.put("formAway",coderField.getFormAway());
        fieldMap.put("isSort",coderField.getIsSort());
        fieldMap.put("searchWay",coderField.getSearchWay());
        fieldMap.put("remark",coderField.getFieldRemark());
        fieldMap.put("isForm",coderField.getIsForm());

        if(coderField.getFieldType().equals("DATETIME")){
            createMap.put("DATEIMPORT","import java.util.Date;");
        }
        return fieldMap;
    }


    public static void createPrimary(List<CoderField> coderFields, Map<String, Object> createMap) {
        for (CoderField coderField : coderFields) {
            if(coderField.getIsPrimary()==1){
                createMap.put("primary",coderField.getFieldName());
                createMap.put("upPrimary",MyStringChangeUtil.changeField(coderField.getFieldName(),3));
                createMap.put("primaryType",coderField.getFieldType());
                createMap.put("primaryClassName",coderField.getClassFieldName());
                createMap.put("primaryClassType",MyStringChangeUtil.changeFileld(coderField.getFieldType()));
            }
        }
    }

    public static void createBaseInfo(CoderTable coderTable, Map<String, Object> createMap) {
        //设置实体对象要生成的包名
        createMap.put("packageName",PathUtil.createPackageName(coderTable));
        //设置对象名称
        createMap.put("className",coderTable.getModifyName());
        //设置表名
        createMap.put("tableName",coderTable.getTableName());
        //设置响应对象的名称classPageName
        createMap.put("classPageName",coderTable.getModifyName()+"Vo");
        //设置请求对象的名称classSearchName
        createMap.put("classSearchName",coderTable.getModifyName()+"QueryDto");
        createMap.put("lowerClassSearchName",StringUtils.uncapitalize(coderTable.getModifyName())+"QueryDto");

        //设置首字母小写lowerClassName
        createMap.put("lowerClassName",StringUtils.uncapitalize(coderTable.getModifyName()));
        //设置创建对象
        createMap.put("saveClassName",coderTable.getModifyName()+"InputDto");
        createMap.put("lowerSaveClassName",StringUtils.uncapitalize(coderTable.getModifyName())+"InputDto");

        createMap.put("remark",coderTable.getRemark());
        //设置对象的是否有工作流的操作
       /* createMap.put("isJbpm",coderTable.getIsProcess()+"");*/

        String packageName=coderTable.getPackageName().replace(".","-");
        String[] packageNames=packageName.split("-");
        StringBuilder basePackageName=new StringBuilder();

        for (int i = 0; i < packageNames.length; i++) {
            if(i!=packageNames.length-1){
                basePackageName.append(packageNames[i]+".");
            }
        }

      /*  String  baseStr=basePackageName.substring(0,basePackageName.length()-1);*/
        createMap.put("basePackageName",coderTable.getPackageName());
    }

    public static void createFbBaseInfo(CoderTable coderTable, Map<String,Object> createMap) {
        //设置实体对象要生成的包名
        if(StringUtils.isNotEmpty(coderTable.getModalName())) {
            createMap.put("packageName", coderTable.getPackageName()+"."+coderTable.getModalName());
        }else{
            createMap.put("packageName", coderTable.getPackageName());
        }
        //设置对象名称
        createMap.put("className",coderTable.getModifyName());
        //设置表名
        createMap.put("tableName",coderTable.getTableName());
        //设置响应对象的名称classPageName
        createMap.put("classPageName",coderTable.getModifyName()+"Vo");
        //设置请求对象的名称classSearchName
        createMap.put("classSearchName",coderTable.getModifyName()+"QueryDto");
        createMap.put("lowerClassSearchName",StringUtils.uncapitalize(coderTable.getModifyName())+"QueryDto");

        //设置首字母小写lowerClassName
        createMap.put("lowerClassName",StringUtils.uncapitalize(coderTable.getModifyName()));
        //设置创建对象
        createMap.put("saveClassName",coderTable.getModifyName()+"InputDto");
        createMap.put("lowerSaveClassName",StringUtils.uncapitalize(coderTable.getModifyName())+"InputDto");

        createMap.put("remark",coderTable.getRemark());
        //设置对象的是否有工作流的操作
        /* createMap.put("isJbpm",coderTable.getIsProcess()+"");*/

        String packageName=coderTable.getPackageName().replace(".","-");
        String[] packageNames=packageName.split("-");
        StringBuilder basePackageName=new StringBuilder();

        for (int i = 0; i < packageNames.length; i++) {
            if(i!=packageNames.length){
                basePackageName.append(packageNames[i]+".");
            }
        }

        String  baseStr=basePackageName.substring(0,basePackageName.length()-1);
        createMap.put("basePackageName",baseStr);
    }
}
